package cn.kgc.day05.listeners;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * @author 课工场
 * @date 2024/10/18
 * @description   该类的实例用户监听session域对象中值的改变
 */
@WebListener
public class CustomerSessionAttributeListener implements HttpSessionAttributeListener {

    //  session中有新的key添加
    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        System.out.println("------------attributeAdded---------------");
        // 在线人数  username
        String name = event.getName();
        System.out.println("key = " + name);
        if ("username".equals(name)){
            // 在线用户数的调整
        }
    }

    //  session中有key被移除
    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        System.out.println("------------attributeRemoved---------------");
        String name = event.getName();
        System.out.println("key = " + name);
        if ("username".equals(name)){
            // 在线用户数的调整
        }
    }

    //  session中有key被替换
    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {
        System.out.println("------------attributeReplaced---------------");
    }
}
